package com.jar.exercise.多线程.线程池.threadpool;

import java.util.concurrent.*;

/**
 * 拒绝策略使用
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(3);
        ThreadFactory factory = r -> new Thread(r, "test-thread-pool");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5,
                0L, TimeUnit.SECONDS, queue, factory,new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 10; i++) {

            executor.submit(() -> {
                try {
                    System.out.println(queue.size());
                    System.out.println("当前线程："+Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            executor.shutdown();
        }
    }
}